package com.baohulu.baohulu.activity.userprofile;

import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

import com.baohulu.baohulu.R;
import com.baohulu.baohulu.base.BaseActivity;
import com.baohulu.baohulu.base.BaseResponseCallback;
import com.baohulu.baohulu.common.Const;
import com.baohulu.baohulu.utils.HttpUtil;
import com.baohulu.baohulu.utils.JsonUtil;
import com.baohulu.baohulu.utils.SharedPrefUtil;
import com.baohulu.baohulu.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdateNickNameActivity extends BaseActivity {
    private static final String TAG = "UpdateNickNameActivity";

    @BindView(R.id.et_nick_name)
    EditText etNickName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_nick_name);
        ButterKnife.bind(this);
        String nickname = SharedPrefUtil.getString(Const.SpKey.LOGIN_USER_NICKNAME);
        etNickName.setText(nickname);
        etNickName.setSelection(nickname.length());
    }

    @OnClick(R.id.confirm)
    public void confirmUpdate() {
        final String nickname = etNickName.getText().toString();
        if (isNicknameValid(nickname)) {
            updateNickname(nickname);
        } else {
            ToastUtil.showToast("输入昵称不合法：" + nickname);
        }
    }

    private boolean isNicknameValid(String nickname) {
        String nicknamePatter = "^[\\u4E00-\\u9FA5A-Za-z0-9_\\-]{4,16}$";
        Pattern p = Pattern.compile(nicknamePatter);
        Matcher matcher = p.matcher(nickname);
        return matcher.matches();
    }

    private void updateNickname(final String nickname) {
        String url = getString(R.string.url_base) + getString(R.string.url_update_nickname);
        JSONObject data = new JsonUtil.JsonBuilder()
                .put("new_name", nickname)
                .build();
        HttpUtil.post(this, url, data, new BaseResponseCallback() {
            @Override
            public void onStart() {
                showProgressDialog();
            }

            @Override
            public void onJsonObjectResponse(JSONObject jsonObject) {
                super.onJsonObjectResponse(jsonObject);
                hideProgressDialog();
                try {
                    int state = jsonObject.getInt("state");
                    if (state == 1) {
                        SharedPrefUtil.putString(Const.SpKey.LOGIN_USER_NICKNAME, nickname);
                        ToastUtil.showToast("修改成功！");
                        finish();
                    } else {
                        ToastUtil.showToast("修改失败，请重试！");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String msg) {
                hideProgressDialog();
                ToastUtil.showToast(msg);
                Log.e(TAG, "onFailure: " + msg);
            }
        });
    }

}
